package com.ihunanren.biz.taobao.spider;

import java.util.Timer;
import java.util.TimerTask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.collect.Sets;
import com.ihunanren.common.spider.DefaultWebSpider;
import com.ihunanren.common.spider.WebSpiderListener;
import com.ihunanren.common.spider.WebSpiderParam;

public class TaobaoConponsTimeTask implements InitializingBean{
	@Autowired
	private WebSpiderListener taobaoWebSpiderListener;
	private final Logger log = LoggerFactory.getLogger(TaobaoConponsTimeTask.class);
	@Override
	public void afterPropertiesSet() throws Exception {
		log.warn("成功注册时间程序：TaobaoConponsTimeTask");
		Timer timer = new Timer();
		//FIXME 
        timer.schedule(new MyTask(), 1000*60*24*60, 1000*60*60*3);//在5分钟后执行此任务,每次间隔3小时
	}
	
	class MyTask extends TimerTask{
		private String[] urlArray = new String[] { "http://tmall.com",
				"http://brand.tmall.com/", "http://temai.tmall.com/",
				"http://taoquan.taobao.com/coupon/index.htm",
				"http://ju.taobao.com/",
				"http://ju.taobao.com/tg/ju_brand_list.htm" };

		@Override
		public void run() {
			log.warn("start TimerTask：TaobaoConponsTimeTask");
			try {
				for(String httpLink:urlArray){
					WebSpiderParam param = new WebSpiderParam();
					param.setBaseUrl(httpLink);
					param.setDeep(2);
					param.setThread(3);
					param.setListeners(Sets.newHashSet(taobaoWebSpiderListener));
					new DefaultWebSpider(param).doSpider();
				}
			} catch (Exception e) {
				log.error("SearchCouponsByLink catch a exception----->",e);
			}
		}
	}
	
}
